
import Element from './Element';

function createElement(type,props,children){
    return new Element(type,props,children);
}

function render(vDom){
    const {type,props,children} = vDom;
    const el = document.createElement(type); // 创建dom节点

    // 设置属性
    for(let key in props){
        el.setAttribute(key,props[key]);
    }
    // 循环子级的数据
    childrenNodeWalk(el,children);
    return el;
}

// 循环子级元素
function childrenNodeWalk(el,children){
    if(children && Array.isArray(children)){
        children.forEach(child=>{
            child instanceof Element ? el.appendChild(render(child)) : el.textContent = child;
        });
    }
}
// 渲染真实页面
function renderDOM(rDom,container){
    container.appendChild(rDom);
}
export {
    createElement,
    render,
    renderDOM
}